//
//  RDBeginController.m
//  readApp
//
//  Created by 朱献国 on 2018/8/16.
//  Copyright © 2018年 😝😝😝😝😝. All rights reserved.
//

#import "RDBeginController.h"
#import "RDReader.h"
#import "RDTopSettingView.h"
#import "RDBottomSettingView.h"

@interface RDBeginController ()
<
RDReaderDelegate,
RDTopSettingViewDelegate,
RDBottomSettingViewDelegate
>

@property (nonatomic, strong) UIView *containerView;

@end

@implementation RDBeginController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)beginRead:(UIButton *)sender {
  
  RDReader *reader = [[RDReader alloc] init];
  
  RDConfiguration *config = [[RDConfiguration alloc] init];
  config.readerBGImage = [UIImage imageNamed:@"yellow"];
  reader.config = config;
  reader.delegate = self;
  
  [self presentViewController:reader animated:YES completion:nil];
  
}

#pragma mark - Event
- (void)settingViewClicked:(UITapGestureRecognizer *)ges {
  UIView *containerView = ges.view;
  RDTopSettingView *topSettingView = [containerView.subviews firstObject];
  RDBottomSettingView *bottomSettingView = [containerView.subviews lastObject];
  
  CGPoint gesPoint = [ges locationInView:containerView];
  CGRect validRect = CGRectMake(0, topSettingView.height, KSCREEN_WIDTH, KSCREEN_HEIGHT - topSettingView.height - bottomSettingView.height);
  
  if (CGRectContainsPoint(validRect, gesPoint)) {
    [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:1 initialSpringVelocity:6 options:UIViewAnimationOptionCurveLinear animations:^{
      topSettingView.transform = CGAffineTransformIdentity;
      bottomSettingView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
      [containerView removeFromSuperview];
    }];
  }
  
}

#pragma mark - RDTopSettingViewDelegate
- (void)topSettingView:(RDTopSettingView *)topSettingView clickedIndex:(TopSettingClickType)clickType {
  switch (clickType) {
    case TopSettingClickTypeReadOver: {
      NSLog(@"退出阅读器");
      [self dismissViewControllerAnimated:YES completion:nil];
      [self.containerView removeFromSuperview];
    }
      break;
      
    default:
      break;
  }
}

#pragma mark - RDBottomSettingViewDelegate
- (void)bottomSettingView:(RDBottomSettingView *)bottomSettingView clickedIndex:(BottomSettingClickType)clickType {
  
}

#pragma mark - RDReaderDelegate
- (void)readerSettingClicked:(RDReader *)reader {
  // 顶部
  RDTopSettingView *topSettingView = [[[NSBundle mainBundle] loadNibNamed:@"RDTopSettingView" owner:nil options:nil] firstObject];
  topSettingView.delegate = self;
  topSettingView.frame = CGRectMake(0, -64, self.view.bounds.size.width, 64);
  
  // 底部
  RDBottomSettingView *bottomSettingView = [[[NSBundle mainBundle] loadNibNamed:@"RDBottomSettingView" owner:nil options:nil] firstObject];
  bottomSettingView.delegate = self;
  bottomSettingView.frame = CGRectMake(0, KSCREEN_HEIGHT, self.view.bounds.size.width, 109);
  
  // 容器
  UIView *containerView = [[UIView alloc] initWithFrame:self.view.bounds];
  self.containerView = containerView;
  [containerView addSubview:topSettingView];
  [containerView addSubview:bottomSettingView];
  [containerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingViewClicked:)]];
  
  // 添加到 window
  [KMainWindow addSubview:containerView];
  [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:3 options:UIViewAnimationOptionCurveLinear animations:^{
    topSettingView.transform = CGAffineTransformMakeTranslation(0, topSettingView.height);
    bottomSettingView.transform = CGAffineTransformMakeTranslation(0, -bottomSettingView.height);
  } completion:nil];
  
}


@end
